package com.tanhua.api.mongo;

import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.domain.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@Service
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    // 此方法要提供：点赞、评论、喜欢保存
    @Override
    public Integer save(Comment comment) {
        // 1、保存comment对象
        comment.setCreated(System.currentTimeMillis()); // 创建时间
        Publish publish = mongoTemplate.findById(comment.getPublishId(), Publish.class);
        comment.setPublishUserId(publish.getUserId()); // 指定动态用户id
        mongoTemplate.save(comment);

        // 2、对动态数量+1
        Query query = new Query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        // 判断动态类型
        update.inc(comment.getCol(), 1);
        mongoTemplate.updateFirst(query, update, Publish.class);

        // 3、查询动态评论数量
        Query countQuery = new Query(
                Criteria.where("publishId").is(comment.getPublishId())
                        .and("commentType").is(comment.getCommentType())
        );

        Long total = mongoTemplate.count(countQuery, Comment.class);

        return total.intValue();
    }

    // 此方法要提供：点赞、评论、喜欢 删除
    @Override
    public Integer remove(Comment comment) {

        // 1、删除comment对象
        Query query = new Query(
                Criteria.where("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType())
                .and("userId").is(comment.getUserId())
        );
        mongoTemplate.remove(query, Comment.class);


        // 2、动态数量-1
        Query updateQuery = new Query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        // 判断动态类型
        update.inc(comment.getCol(), -1);
        mongoTemplate.updateFirst(updateQuery, update, Publish.class);


        // 3、返回comment数量
        Query countQuery = new Query(
                Criteria.where("publishId").is(comment.getPublishId())
                        .and("commentType").is(comment.getCommentType())
        );

        Long total = mongoTemplate.count(countQuery, Comment.class);
        return total.intValue();
    }

    @Override
    public PageBeanVo findByPage(Integer pageNum, Integer pageSize,String publishId) {

        // 分页查询评论列表
        Integer index = (pageNum - 1) * pageSize;
        Query query = new Query(
                Criteria.where("publishId").is(new ObjectId(publishId))
                .and("commentType").is(2)
        ).with(Sort.by(Sort.Order.desc("created")))
                .skip(index).limit(pageSize);
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);

        long counts = mongoTemplate.count(query, Comment.class);

        return new PageBeanVo(pageNum, pageSize, counts, commentList);
    }

    @Override
    public PageBeanVo findCommentByPage(Integer pageNum, Integer pageSize, Integer commentType, Long publishUserId) {
        // 1、分页条件查询
        Integer index = (pageNum-1)*pageSize;

        Query query = Query.query(Criteria.where("commentType").is(commentType).and("publishUserId").is(publishUserId))
                .skip(index).limit(pageSize);

        List<Comment> commentList = mongoTemplate.find(query, Comment.class);

        long counts = mongoTemplate.count(query, Comment.class);

        // 2、返回分页对象
        return new PageBeanVo(pageNum, pageSize, counts, commentList);
    }
}
